#ifndef _LOSS_H_
#define _LOSS_H_
#include "reg52.h"

#define doubleCrossingNotCenter 0
#define doubleCrossing 1
#define LeftCrossing 2
#define RightCrossing 3
#define None 4

typedef unsigned char u8;

//中间的两个
sbit c01=P1^3;
sbit c02=P1^4;
//左边
sbit c1=P1^5;
sbit c2=P1^6;
sbit c3=P1^7;
//右边
sbit c_1=P1^0;
sbit c_2=P1^1;
sbit c_3=P1^2;

//整体逻辑为：判断是否是交叉点(isCrossing)->不是判断是否在轨(isOnline)->不在轨计算误差(getLoss)并修正(resetV)
u8 isOnline()
{
	return c01&c02;
}

int getLeftLoss(void)
{
	return (int)c01+2*(int)c1+3*(int)c2+4*(int)c3;
}

int getRightLoss(void)
{
	return (int)c01+2*(int)c_1+3*(int)c_2+4*(int)c_3;
}

int getLoss(void)
{
	return getLeftLoss()-getRightLoss();
}

u8 isCrossing(void)
{
	int leftloss=getLeftLoss();
	int rightloss=getRightLoss();
	if(isOnline()) //中间两个传感器都检测到，说明之前正在轨道上
	{
		if(leftloss<=0&rightloss<=0)
			return doubleCrossing;
		else
		{
			if(leftloss<=0)
				return LeftCrossing;
			if(rightloss<=0)
				return RightCrossing;
		}
	}
	else
	{
		if(leftloss<=0&rightloss<=0)
			return doubleCrossingNotCenter;
	}
	return None; //正常在轨
}

#endif